package cn.com.blueInfo.bpm.number.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.number.entity.NumberRuleComponent;
import cn.com.blueInfo.bpm.number.enums.NumberRuleComponentType;
import cn.com.blueInfo.bpm.number.mapper.NumberRuleComponentMapper;
import cn.com.blueInfo.bpm.number.repository.NumberRuleComponentRepository;
import cn.com.blueInfo.bpm.number.request.NumberRuleComponentDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 编号规则组成配置表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:26:29
 */
@Log4j2
@Service
public class NumberRuleComponentRepositoryImpl
        extends ServiceImpl<NumberRuleComponentMapper, NumberRuleComponent>
        implements NumberRuleComponentRepository, BaseRepository<NumberRuleComponent, NumberRuleComponentDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增编号规则组成配置
     * @param numberRuleComponent 编号规则组成配置参数
     */
    @Override
    public Integer createNumberRuleComponent(NumberRuleComponent numberRuleComponent) {
        return sqlExecuteResult(save(numberRuleComponent));
    }

    /**
     * 批量新增编号规则组成配置
     * @param numberRuleComponentList 编号规则组成配置参数列表
     */
    @Override
    public Integer batchCreateNumberRuleComponent(List<NumberRuleComponent> numberRuleComponentList) {
        return sqlExecuteResult(saveBatch(numberRuleComponentList), numberRuleComponentList.size());
    }

    /**
     * 删除编号规则组成配置
     * @param numberRuleComponentId 编号规则组成配置ID
     */
    @Override
    public Integer deleteNumberRuleComponent(Long numberRuleComponentId) {
        NumberRuleComponent numberRuleComponent = new NumberRuleComponent();
        numberRuleComponent.setId(numberRuleComponentId);
        numberRuleComponent.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(numberRuleComponent));
    }

    /**
     * 批量删除编号规则组成配置
     * @param numberRuleComponentIdList 编号规则组成配置ID列表
     */
    @Override
    public Integer batchDeleteNumberRuleComponent(List<Long> numberRuleComponentIdList) {
        List<NumberRuleComponent> numberRuleComponentList = new ArrayList<>();
        for (Long numberRuleComponentId : numberRuleComponentIdList) {
            NumberRuleComponent numberRuleComponent = new NumberRuleComponent();
            numberRuleComponent.setId(numberRuleComponentId);
            numberRuleComponent.setDeleted(BaseDeleted.DELETED);
            numberRuleComponentList.add(numberRuleComponent);
        }
        return sqlExecuteResult(removeBatchByIds(numberRuleComponentList), numberRuleComponentList.size());
    }

    /**
     * 更新部分编号规则组成配置
     * @param numberRuleComponent 编号规则组成配置参数
     */
    @Override
    public Integer updateNumberRuleComponentPartial(NumberRuleComponent numberRuleComponent) {
        validateUpdateId(numberRuleComponent.getId());
        LambdaUpdateWrapper<NumberRuleComponent> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NumberRuleComponent::getId, numberRuleComponent.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部编号规则组成配置
     * @param numberRuleComponent 编号规则组成配置参数
     */
    @Override
    public Integer updateNumberRuleComponent(NumberRuleComponent numberRuleComponent) {
        validateUpdateId(numberRuleComponent.getId());
        return sqlExecuteResult(updateById(numberRuleComponent));
    }

    /**
     * 查询一条编号规则组成配置
     * @param numberRuleComponentId 编号规则组成配置ID
     */
    @Override
    public NumberRuleComponent getNumberRuleComponentById(Long numberRuleComponentId) {
        LambdaQueryWrapper<NumberRuleComponent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NumberRuleComponent::getId, numberRuleComponentId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询编号规则组成配置
     * @param numberRuleComponentQueryDTO 编号规则组成配置参数
     */
    @Override
    public Page<NumberRuleComponent> getNumberRuleComponentPage(
            NumberRuleComponentDTO.QueryDTO numberRuleComponentQueryDTO) {
        Page<NumberRuleComponent> numberRuleComponentPage = new Page<>(
                numberRuleComponentQueryDTO.getPage(), numberRuleComponentQueryDTO.getSize());
        LambdaQueryWrapper<NumberRuleComponent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, numberRuleComponentQueryDTO);

        BaseDeleted deleted = numberRuleComponentQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(numberRuleComponentPage, queryWrapper);
        } else {
            return page(numberRuleComponentPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 编号规则组成配置 数量
     * @param numberRuleComponentQueryDTO 编号规则组成配置参数
     */
    @Override
    public Long getNumberRuleComponentCount(NumberRuleComponentDTO.QueryDTO numberRuleComponentQueryDTO) {
        LambdaQueryWrapper<NumberRuleComponent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, numberRuleComponentQueryDTO);

        BaseDeleted deleted = numberRuleComponentQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 编号规则组成配置
     * @param numberRuleComponentQueryDTO 编号规则组成配置参数
     */
    @Override
    public List<NumberRuleComponent> getNumberRuleComponentList(
            NumberRuleComponentDTO.QueryDTO numberRuleComponentQueryDTO) {
        LambdaQueryWrapper<NumberRuleComponent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, numberRuleComponentQueryDTO);

        BaseDeleted deleted = numberRuleComponentQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<NumberRuleComponent> queryWrapper,
                              NumberRuleComponentDTO.QueryDTO numberRuleComponentQueryDTO) {

        Long ruleId = numberRuleComponentQueryDTO.getRuleId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(ruleId), NumberRuleComponent::getRuleId, ruleId);

        NumberRuleComponentType componentType = numberRuleComponentQueryDTO.getComponentType();
        queryWrapper.eq(ObjectUtils.isNotEmpty(componentType), NumberRuleComponent::getComponentType, componentType);

        String componentContent = numberRuleComponentQueryDTO.getComponentContent();
        queryWrapper.eq(StringUtils.isNotBlank(componentContent), NumberRuleComponent::getComponentContent, componentContent);

        Byte length = numberRuleComponentQueryDTO.getLength();
        queryWrapper.eq(ObjectUtils.isNotEmpty(length), NumberRuleComponent::getLength, length);
        // 添加公共查询字段
        addQueryWrapper(queryWrapper, numberRuleComponentQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<NumberRuleComponent, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                NumberRuleComponent::getOrderNumber,
                NumberRuleComponent::getTenantId,
                NumberRuleComponent::getApplicationId,
                NumberRuleComponent::getStatus,
                NumberRuleComponent::getDeleted,
                NumberRuleComponent::getDeleteUserId,
                NumberRuleComponent::getDeleteTime,
                NumberRuleComponent::getRemark,
                NumberRuleComponent::getCreateUserId,
                NumberRuleComponent::getCreateTime,
                NumberRuleComponent::getUpdateUserId,
                NumberRuleComponent::getUpdateTime,
                NumberRuleComponent::getExtend1,
                NumberRuleComponent::getExtend2,
                NumberRuleComponent::getExtend3);
    }

}
